package com.qsl.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qsl.model.system.SysUser;
import com.qsl.model.vo.RouterVo;
import com.qsl.model.vo.SysUserQueryVo;
import com.qsl.system.mapper.SysUserMapper;
import com.qsl.system.service.SysMenuService;
import com.qsl.system.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysMenuService sysMenuService;

    @Override
    public IPage<SysUser> selectPage(Page<SysUser> pageParam, SysUserQueryVo sysUserQueryVo) {
        IPage<SysUser> pageMode = sysUserMapper.selectPage(pageParam, sysUserQueryVo);
        return pageMode;
    }

    /**
     * 查询用户是否存在
     *
     * @param username
     * @return
     */
    @Override
    public SysUser getUserInfoByUserName(String username) {
        QueryWrapper<SysUser> qw = new QueryWrapper<>();
        qw.eq("username", username);

        return sysUserMapper.selectOne(qw);
    }

    // 根据用户名称获取用户信息（基本信息 和菜单权限 和按钮权限数据）
    @Override
    public Map<String, Object> getUserinfo(String username) {
        System.out.println("@@@================>" + username);
        // 根据username查询用户基本信息
        SysUser sysUser = this.getUserInfoByUserName(username);
        // 根据userId查询菜单权限值
        List<RouterVo> routerVoList = sysMenuService.getUserMenuList(sysUser.getId());
        // 根据userId查询按钮权限值
        List<String> permsList = sysMenuService.getUserButtonList(sysUser.getId());

        Map<String, Object> result = new HashMap<>();
        result.put("name", username);
        result.put("avatar", "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        result.put("roles", "[\"admin\"]");
        result.put("routers", routerVoList); // 路由权限
        result.put("buttons", permsList); // 按钮权限
        return result;
    }
}
